//
//  ZDTextView.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/11/5.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDTextView.h"
#import <Masonry/Masonry.h>

@interface ZDTextView ()

//占位label
@property (nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation ZDTextView

- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer
{
    if (self = [super initWithFrame:frame textContainer:textContainer])
    {
        [self prepareUI];
    }
    
    return self;
}


- (void)prepareUI
{
    [self addSubview:self.placeholderLabel];
    
    [_placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.mas_left).offset(5);
        make.top.mas_equalTo(self.mas_top).offset(8);
    }];
    
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
}

//注销通知
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//通知回调方法
- (void)textDidChange
{
    //开始编辑的时候隐藏占位文本  
    _placeholderLabel.hidden = self.hasText;
}

//设置占位文本
- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    
    _placeholderLabel.font = self.font;
    
    if (placeholder)
    {
        _placeholderLabel.text = placeholder;
    }
}



#pragma mark 懒加载
- (UILabel *)placeholderLabel
{
    if (!_placeholderLabel)
    {
        _placeholderLabel = [[UILabel alloc] init];
        
        _placeholderLabel.textColor = [UIColor lightGrayColor];
        
        //设置默认占位文本
        _placeholderLabel.text = @"share your enjoy...";
    }
    
    return _placeholderLabel;
}


@end
